Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Java Variables

Types of variables

Local Variables

Local variables are declared within constructors, methods, or blocks. They are only accessible within the scope where they are defined. Their scope is limited to the block of code in which they are declared. Local variables must be initialized before they are used, and they don't have default values. Example of a local variable:
Local variable Example
public class Main{ public static void main(String args[]) { int a = 10; // 'a' is a local variable int b = 20; int sum = a + b; System.out.println("Sum: " + sum); } }

Output

Sum: 30
Java local variable example
public class Main{ public static void main(String[] args) { int x = 10; System.out.println(x); } }

Output

10
Instance Variables Instance variables, also known as fields, are declared within a class but outside any method. They are associated with objects created from the class and have different values for each object. Instance variables hold data that represents the state of an object and can have default values if not explicitly initialized. They exist as long as the object exists. Example of instance variables:
Instance variable in Java -Student information display
public class Main { public static void main(String[] args) { // Create a new Student object Student student1 = new Student("John Doe", 20); // Display student information student1.displayInfo(); // Update student's name and age student1.setName("Jane Smith"); student1.setAge(21); // Display updated student information System.out.println("Updated Info:"); student1.displayInfo(); } } public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void displayInfo() { System.out.println("Name: " + name + ", Age: " + age); } }

Output

Name: John Doe, Age: 20 Updated Info: Name: Jane Smith, Age: 21
Static Variable Static variables are declared within a class but are marked as static. They belong to the class itself rather than individual objects. Static variables have the same value across all instances of the class and are shared among them. They are initialized when the class is loaded and exist as long as the class is loaded in memory. Example of static variables:
Example of static variable - Bank account and counter value
public class Main { public static void main(String[] args) { Bank account1 = new Bank(12345); Bank account2 = new Bank(67890); account1.displayAccountInfo(); account2.displayAccountInfo(); Counter.increment(); Counter.increment(); System.out.println("Counter Value: " + Counter.getCounter()); Counter.resetCounter(); System.out.println("Counter Value after reset: " + Counter.getCounter()); } } public class Bank { static String bankName = "MyBank"; // Static variable int accountNumber; public Bank(int accountNumber) { this.accountNumber = accountNumber; } public void displayAccountInfo() { System.out.println("Bank: " + bankName + ", Account Number: " + accountNumber); } } public class Counter { private static int counter = 0; public static void increment() { counter++; } public static int getCounter() { return counter; } public static void resetCounter() { counter = 0; } }

Output

Bank: MyBank, Account Number: 12345 Bank: MyBank, Account Number: 67890 Counter Value: 2 Counter Value after reset: 0
Summary: 1. Local variables are declared within methods or blocks and have limited scope within that block. 2. Instance variables (fields) are associated with objects and represent the state of individual objects. 3. Static variables (class variables) belong to the class itself and are shared among all instances of the class. 4. Each type of variable serves a specific purpose in Java programming. Local variables are used for temporary storage within methods, instance variables represent the state of objects, and static variables provide shared data across instances of a class. Understanding these variable types helps in designing efficient and organized Java programs.

  📌TAGS

★variables in Java ★variable types ★Java variables ★java

Tutorials